package com.auth.token.base;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.UUID;

/**
 * @author: 王霄
 * @date: 2022/1/20 18:24
 */
public class TokenUuidMd5Generator implements TokenGenerator {

    private static final ThreadLocal<MessageDigest> MD5_LOCAL = ThreadLocal.withInitial(() -> {
        try {
            return MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("未找到MD5算法", e);
        }
    });

    @Override
    public String generate() {
        String uuid = UUID.randomUUID().toString();
        MessageDigest digest = MD5_LOCAL.get();
        digest.reset();
        byte[] bs = digest.digest(uuid.getBytes());
        return Base64.getEncoder().encodeToString(bs);
    }
}
